﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WITM.DAL;

namespace WITM.BLL
{
    public class SpendingItemBL
    {
        private IUnitOfWork unitOfWork;

        public SpendingItemBL(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }

        public IEnumerable<SpendingItems> GetAll()
        {
            return unitOfWork.SpendingItemRepository.GetAll();
        }

        public IEnumerable<SpendingItems> GetAllPerCategory(long categoryID)
        {
            return unitOfWork.SpendingItemRepository.Find(i => i.SpendingCategories.CategoryID == categoryID);
        }
        
        public SpendingItems GetItemByID(long itemID)
        {
            return unitOfWork.SpendingItemRepository.First(i => i.ItemID == itemID);
        }

        public IEnumerable<SpendingItems> GetAllPerUser(int userID, string filter)
        {
            return unitOfWork.SpendingItemRepository.Find(i => i.SpendingCategories.Users.UserID == userID
                && (i.SpendingCategories.Description.Contains(filter) || i.Description.Contains(filter)), true);
        }

        public void Add(SpendingItems item)
        {
            SpendingItems checkItem = unitOfWork.SpendingItemRepository.First(
                    i => i.SpendingCategories.CategoryID == item.SpendingCategories.CategoryID
                        && i.Description == item.Description);
            if (checkItem == null)
            {
                unitOfWork.SpendingItemRepository.Add(item);
                unitOfWork.Commit();
            }
            else
            {
                throw new Exception("Item with specific name already exists for that category");
            }
        }
    }
}
